package no.netlight.gotogate;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class RouteList extends Activity {
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);

		ListView listview = new ListView(this);
		
		registerForContextMenu(listview);
		
		View mainpanel = PanelBuilder.createList(RouteList.this, listview);
		
		setContentView(mainpanel);

	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.route_context_menu, menu);
	}

	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.menu.route_context_menu:
			
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	public static class PanelBuilder {
		public static View createList(Activity activity, ListView lv) {

			// create the ui components
			LinearLayout mainpanel = new LinearLayout(activity);
			ListView listview = lv;
			
			// setup the mainpanel
			{
				// apply view group params for the activity's root pane
				LayoutUtils.Layout.WidthFill_HeightFill
						.applyViewGroupParams(mainpanel);
				// set animation layout on the mainpanel
				AnimUtils.setLayoutAnim_slideupfrombottom(mainpanel, activity);
			}

			// setup the listview and add to the mainpanel
			{
				LayoutUtils.Layout.WidthFill_HeightFill
						.applyLinearLayoutParams(listview);

				bindListViewToAdapter(activity, listview);

				AnimUtils.setLayoutAnim_slidedownfromtop(listview, activity);

				mainpanel.addView(listview);
			}

			// return the mainpanel
			return mainpanel;

		}

		/** create the list data, and bind a custom adapter to the listview */
		private static void bindListViewToAdapter(Activity ctx,
				ListView listview) {

			final RouteListAdapter listModelView = new RouteListAdapter(ctx,
					listview);

			// bind a selection listener to the view
			listview
					.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
						public void onItemSelected(AdapterView parentView,
								View childView, int position, long id) {
							listModelView.setSelected(position);
						}

						public void onNothingSelected(AdapterView parentView) {
							listModelView.setSelected(-1);
						}
					});

		}
		
		
	}
}